import 'package:flutter/material.dart';
import 'package:jtsell_app/entity/mall_goods_entity.dart';
import 'package:jtsell_app/network/http_dispense.dart';
import 'package:jtsell_app/provider/test_stream_provider_model.dart';
import 'package:provider/provider.dart';

class FutureProviderPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<MallGoodsEntity> initList = [MallGoodsEntity(goodsId: "init商品")];

    return FutureProvider<List<MallGoodsEntity>>(
      builder: (BuildContext context) {
        return HttpDispense.getMallGoodsList({"page": 1});
      },
      child: CountWidget(),
      initialData: initList,
    );
  }
}

class CountWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: <Widget>[ButtonWidget(), TextWidget()],
    ));
  }
}

class ButtonWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: FlatButton.icon(
          icon: Icon(Icons.add),
          label: Text("获取菜单名"),
          onPressed: () {
            MallGoodsEntity tempMallGoodsEntity =
                MallGoodsEntity(goodsId: "测试商品");
            Provider.of<List<MallGoodsEntity>>(context).forEach((item){
              print(item.goodsName);
            });
          }),
    );
  }
}

class TextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Consumer<List<MallGoodsEntity>>(builder:
          (BuildContext context, List<MallGoodsEntity> value, Widget child) {
        print(1111);
        if (value != null) {
          value.forEach((item) {
            print(item.goodsName);
          });
        }

        return Text("111");
      }),
    );
  }
}
